home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / _gs / !GS / c / ZPAINT < prev    next >
Text File  |  1991-10-25  |  6KB  |  204 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zpaint.c */
  21. /* Painting operators for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "alloc.h"
  26. #include "estack.h"            /* for image[mask] */
  27. #include "store.h"
  28. #include "gsmatrix.h"
  29. #include "gspaint.h"
  30. #include "state.h"
  31.  
  32. /* Forward references */
  33. private int image_setup(P3(os_ptr, int, int));
  34. private int image_continue(P1(os_ptr));
  35.  
  36. /* erasepage */
  37. int
  38. zerasepage(register os_ptr op)
  39. {    return gs_erasepage(igs);
  40. }
  41.  
  42. /* fill */
  43. int
  44. zfill(register os_ptr op)
  45. {    return gs_fill(igs);
  46. }
  47.  
  48. /* eofill */
  49. int
  50. zeofill(register os_ptr op)
  51. {    return gs_eofill(igs);
  52. }
  53.  
  54. /* stroke */
  55. int
  56. zstroke(register os_ptr op)
  57. {    return gs_stroke(igs);
  58. }
  59.  
  60. /* colorimage */
  61. int
  62. zcolorimage(register os_ptr op)
  63. {    int spp;            /* samples per pixel */
  64.     int npop = 7;
  65.     os_ptr procp = op - 2;
  66.     int code;
  67.     check_type(*op, t_integer);    /* ncolors */
  68.     check_type(op[-1], t_boolean);    /* multiproc */
  69.     if ( (ulong)(op->value.intval) > 4 ) return e_rangecheck;
  70.     switch ( (spp = (int)(op->value.intval)) )
  71.        {
  72.     case 1:
  73.         pop(2);            /* WRONG if zimage fails */
  74.         return zimage(op);
  75.     case 3: case 4:
  76.         break;
  77.     default:
  78.         return e_rangecheck;
  79.        }
  80.     if ( op[-1].value.index )    /* planar format */
  81.         npop += spp - 1,
  82.         procp -= spp - 1,
  83.         spp = - spp;
  84.     check_type(procp[-2], t_integer);    /* bits/sample */
  85.     if ( (ulong)(procp[-2].value.intval) > 8 ) return e_rangecheck;
  86.     code = image_setup(procp, (int)procp[-2].value.intval, spp);
  87.     if ( code >= 0 ) pop(npop);
  88.     return code;
  89. }
  90.  
  91. /* image */
  92. int
  93. zimage(register os_ptr op)
  94. {    int code;
  95.     check_type(op[-2], t_integer);    /* bits/sample */
  96.     if ( (ulong)(op[-2].value.intval) > 8 ) return e_rangecheck;
  97.     code = image_setup(op, (int)op[-2].value.intval, 1);
  98.     if ( code >= 0 ) pop(5);
  99.     return code;
  100. }
  101.  
  102. /* imagemask */
  103. int
  104. zimagemask(register os_ptr op)
  105. {    int code;
  106.     check_type(op[-2], t_boolean);
  107.     code = image_setup(op, op[-2].value.index, 0);
  108.     if ( code >= 0 ) pop(5);
  109.     return code;
  110. }
  111.  
  112. /* Common setup for [color]image and imagemask. */
  113. /* spp is 0 for imagemask, 1 for image, and [-]3 or [-]4 for colorimage. */
  114. private int
  115. image_setup(os_ptr op, int param3 /* bits/sample or invert */, int spp)
  116. {    int code;
  117.     gs_image_enum *penum;
  118.     gs_matrix mat;
  119.     int px;
  120.     int pmax = (spp < 0 ? ~ spp : 0);
  121.     /* We push on the estack: */
  122.     /*    Control mark, 4 procs, last plane index, */
  123.     /*    enumeration structure (as bytes). */
  124. #define inumpush 7
  125.     check_estack(inumpush + 2);    /* stuff above, + continuation + proc */
  126.     check_type(op[-4], t_integer);    /* width */
  127.     check_type(op[-3], t_integer);    /* height */
  128.     /* Note that the "procedures" might not be procedures, */
  129.     /* but might be literal strings. */
  130.     for ( px = 0; px <= pmax; px++ )
  131.       if ( !r_has_type(op + px, t_string) )
  132.        {    check_proc(op[px]);    /* proc(s) */
  133.        }
  134.     if ( op[-4].value.intval <= 0 || op[-3].value.intval < 0 )
  135.         return e_undefinedresult;
  136.     if ( op[-3].value.intval == 0 ) return 0;    /* empty image */
  137.     if ( (code = read_matrix(op - 1, &mat)) < 0 )
  138.         return code;
  139.     if ( (penum = (gs_image_enum *)alloc(1, gs_image_enum_sizeof, "image_setup")) == 0 )
  140.         return e_VMerror;
  141.     code = (spp == 0 ?
  142.         gs_imagemask_init(penum, igs, (int)op[-4].value.intval,
  143.                   (int)op[-3].value.intval, param3, &mat, 1) :
  144.         gs_image_init(penum, igs, (int)op[-4].value.intval,
  145.                   (int)op[-3].value.intval, param3, spp, &mat) );
  146.     if ( code < 0 ) return code;
  147.     mark_estack(es_other);
  148.     ++esp;
  149.     for ( px = 0; px < 4; esp++, px++ )
  150.       if ( px <= pmax )
  151.         *esp = op[px];
  152.       else
  153.         make_null(esp);
  154.     make_int(esp, 0);        /* current plane */
  155.     r_set_size(esp, pmax);
  156.     ++esp;
  157.     make_tasv(esp, t_string, 0, gs_image_enum_sizeof, bytes, (byte *)penum);
  158.     push_op_estack(image_continue);
  159.     *++esp = *op;            /* run the (first) procedure */
  160.     return o_check_estack;
  161. }
  162. /* Continuation procedure.  Hand the string to the enumerator. */
  163. private int
  164. image_continue(register os_ptr op)
  165. {    gs_image_enum *penum = (gs_image_enum *)esp->value.bytes;
  166.     int code;
  167.     if ( !r_has_type(op, t_string) )
  168.        {    /* Procedure didn't return a string.  Quit. */
  169.         esp -= inumpush;
  170.         alloc_free((char *)penum, 1, gs_image_enum_sizeof,
  171.                "image_continue(quit)");
  172.         return e_typecheck;
  173.        }
  174.     code = gs_image_next(penum, op->value.bytes, r_size(op));
  175.     if ( r_size(op) == 0 || code > 0 )    /* stop now */
  176.        {    esp -= inumpush;
  177.         alloc_free((char *)penum, 1, gs_image_enum_sizeof,
  178.                "image_continue(finished)");
  179.        }
  180.     else
  181.        {    int px = (int)++(esp[-1].value.intval);
  182.         es_ptr pproc = esp - 5;
  183.         if ( px > r_size(esp - 1) )
  184.             esp[-1].value.intval = px = 0;
  185.         push_op_estack(image_continue);
  186.         *++esp = pproc[px];
  187.        }
  188.     pop(1);
  189.     return o_check_estack;
  190. }
  191.  
  192. /* ------ Initialization procedure ------ */
  193.  
  194. op_def zpaint_op_defs[] = {
  195.     {"0eofill", zeofill},
  196.     {"0erasepage", zerasepage},
  197.     {"0fill", zfill},
  198.     {"7colorimage", zcolorimage},
  199.     {"5image", zimage},
  200.     {"5imagemask", zimagemask},
  201.     {"0stroke", zstroke},
  202.     op_def_end(0)
  203. };
  204.